package com.study.mike.common;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @author king james
 * @create 2021-12-10-{TIME}
 * 代码自动生成器
 */
public class AutoCode {
    private static final String JdbcDriver = "com.mysql.cj.jdbc.Driver";
    private static final String url = "jdbc:mysql://118.31.76.49:3306/autoCode?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";
    private static final String userName = "root";
    private static final String passWord = "kanghui";

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        //路径
        gc.setOutputDir(projectPath + "/src/main/java");
        //作者
        gc.setAuthor("KangHui");
        //是否打开
        gc.setOpen(false);
        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        //是否覆盖
        gc.setFileOverride(true);
        //去掉service的I前缀
        gc.setServiceName("BookService");
        //主键自增策略
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);

        mpg.setGlobalConfig(gc);

        //2、设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName(JdbcDriver);
        dsc.setUrl(url);
        dsc.setUsername(userName);
        dsc.setPassword(passWord);

        mpg.setDataSource(dsc);

        //3、配置生成那些包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("common");
        pc.setParent("com.study.mike");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");

        mpg.setPackageInfo(pc);

        //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        //设置要映射的表名 -->支持多个
        strategy.setInclude("book");
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //实体父类
        strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        //自动Lombok
        strategy.setEntityLombokModel(true);
        //逻辑删除策略
        strategy.setLogicDeleteFieldName("delete_flag");
        //自动填充策略
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill modifyTime = new TableFill("modify_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(modifyTime);
        strategy.setTableFillList(tableFills);
        //乐观锁
        strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);

        mpg.setStrategy(strategy);

        mpg.execute();


    }
}
